/**
 * 持仓贡献
 * Created by KY on 2017/4/18.
 */
define(['ldfw/core','echarts','comps/ajax','comps/session','text!tpl/position/contribution.tpl'],
    function(LDFW,echarts,ajax,session,tpl){
    return LDFW.Container.extend({
        tpl:tpl,
        className:'panel-body earnAnalysis',
        initialize:function(){
            const _this = this
            this.chartInit = false
            this.on('shown',function(){
                _this.loadData()
            })
        },
        loadData:function(){
            let _this = this
            ajax.post('pfL.1.205').success(function(resp){
                if(!_.isEmpty(resp.result)){
                    _this.renderChart.call(_this,resp.result)
                }
            })
        },
        initChart:function(){
            let height = window.innerHeight-130; //-padding
            let $chart = this.$el.find('#stockContribution')
            $chart.css('height',height+'px')
            $chart.css('width',this.$el.width()+'px')
            let chart = this.chart = echarts.init($chart[0])
            let option = this.chartOption = {
                tooltip : {
                    trigger: 'axis',
                    axisPointer : {
                        type : 'line'
                    }
                },
                legend: {
                    data:['利润', '成本', '现值'],
                    textStyle: {
                        color: "#b3bcc5"
                    }
                },
                grid: {
                    left: '3%',
                    right:'7%',
                    bottom:'8%',
                    containLabel: true
                },
                yAxis : [
                    {
                        type : 'value',
                        axisLabel :{
                            textStyle:{
                                color:'#b3bcc5'
                            },
                            formatter:function(value,index){
                                value = value/1000
                                if(value===0)return value
                                return value+'k'
                            }
                        },
                        splitLine:{show:false}
                    }
                ],
                xAxis : [
                    {
                        type : 'category',
                        axisTick : {
                            show: true,
                        },
                        axisLine: {onZero: true},
                        axisLabel :{
                            textStyle:{
                                color:'#b3bcc5'
                            }
                        },
                        splitLine: {show: false},
                        nameLocation:'middle'
                    }
                ],
                series : [
                    {
                        name:'利润',
                        type:'bar'

                    },
                    {
                        name:'成本',
                        type:'bar',
                    },
                    {
                        name:'现值',
                        type:'bar'
                    }
                ]
            }
            this.chartInit = true
        },

        renderChart:function(list){
            if(!this.chartInit){
                this.initChart()
            }
            let xAxisData = []
            let profitData = []
            let costData = []
            let priceData =[]
            let option = this.chartOption
            list.forEach(function(item){
                xAxisData.push(item.pd_name)
                profitData.push(item.realize_pandl)
                costData.push((item.posi_cost).toFixed(2))
                priceData.push((item.share_net*item.pd_share).toFixed(2))
            })
            $.extend(true,option,{
                xAxis:[{
                    data:xAxisData
                }],
                series:[{
                    name:'利润',
                    data:profitData
                },{
                    name:'成本',
                    data:costData,
                },{
                    name:'现值',
                    data:priceData
                }]
            })
            this.chart.setOption(option)
        }
    })
})